<?php
/**
 * Created by PhpStorm.
 * User: zqk
 * Date: 2018/11/27
 * Time: 15:50
 */

namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\XhException;

class Licence
{

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 初始化盒子信息
     *
     * @throws XhException
     *
     * create by zqk 20181127
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config = Main::getConfigByName('user');
            self::$url = Tool::getValue($config, 'url'); //许可证服务地址
            if (!self::$url) {
                throw new XhException('许可证服务url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取许可证设备绑定列表
     *
     * @param int $page
     * @param int $size
     * @param null $deviceId
     * @param null $type
     * @param null $status
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by zqk 20181127
     */
    public static function getLicenceDev($page = 1, $size = 50, $deviceId = null, $type = null, $status = null)
    {
        self::init();
        $params = [];
        $params['page'] = $page - 1;
        $params['size'] = $size;
        if (isset($deviceId)) {
            $params['deviceId'] = $deviceId;
        }
        if (isset($type)) {
            $params['type'] = $type;
        }
        if (isset($status)) {
            $params['status'] = $status;
        }
        $response = self::$httpClient->get("/api/v1/pri/devices/license/", $params);
        if (!$response->isSuccess()) {
            throw new XhException('获取许可证设备绑定列表失败', $response);
        }
        $data = $response->toArray();
        return $data;
    }

    /**
     * 激活
     *
     * @param $allocationId
     *
     * @throws XhException
     *
     * create by zqk 20181127
     */
    public static function activeLicence($allocationId)
    {
        self::init();
        $params = [
            'allocationId' => $allocationId,
        ];
        $response = self::$httpClient->patch("/api/v1/pri/devices/license/activate", $params);
        if (!$response->isSuccess()) {
            $response = $response->toArray();
            throw new XhException('激活失败', $response);
        }
    }

    /**
     * 解绑
     *
     * @param $allocationId
     *
     * @throws XhException
     *
     * create by zqk 20181127
     */
    public static function unbindLicence($allocationId)
    {
        self::init();
        $params = [
            'allocationId' => $allocationId,
        ];
        $response = self::$httpClient->patch("/api/v1/pri/devices/license/relieve", $params);
        if (!$response->isSuccess()) {
            $response = $response->toArray();
            throw new XhException('解绑失败', $response);
        }
    }

    /**
     * 绑定
     *
     * @param $devIds
     * @param $key
     * @param $type
     *
     * @throws XhException
     *
     * create by zqk 20181128
     */
    public static function bindLicence($devIds, $key, $type)
    {
        self::init();
        $params = [
            'deviceIds' => $devIds,
            'key' => $key,
            'type' => $type,
        ];
        $response = self::$httpClient->postJson("/api/v1/pri/devices/license/allot", $params);
        if (!$response->isSuccess()) {
            $response = $response->toArray();
            throw new XhException('绑定失败', $response);
        }
    }


}